<template>
  <div class="city-list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.city}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="item in hotCities" :key="item.id" @click="handleChangeCity(item.name)">
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div v-for="(item,key) in cities" :key="key" :ref="key">
          <div class="title border-topbottom">{{key}}</div>
          <div class="item-list">
            <div class="item border-topbottom" v-for="innerItem in item" :key="innerItem.id" @click="handleChangeCity(innerItem.name)">
              {{innerItem.name}}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import BScroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CiytList',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  data: function () {
    return {
      currentCity: ''
    }
  },
  computed: {
    ...mapState(['city'])
  },
  watch: {
    letter: function () {
      if (this.letter) {
        const ele = this.$refs[this.letter][0]
        console.log(ele)
        this.scroll.scrollToElement(ele)
      }
    }
  },
  methods: {
    handleChangeCity (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    //    vuex里的一个mutation：changeCiyt 对应当前组件的一个方法changeCity，
    ...mapMutations(['changeCity'])
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper)
  }
}
</script>
<style lang="stylus" scoped>
  @import "~stylus/varibles.styl"
  .border-topbottom
    &:before
      border-color #ccc
    &:after
      border-color #ccc
  .city-list
    position: absolute
    top 1.58rem
    left 0rem
    right 0rem
    bottom 0rem
    overflow hidden
  .title
    height .5rem
    line-height .5rem
    background-color #eee
    padding-left .2rem
  .button-list
    width 100%
    overflow hidden
    padding .1rem .6rem .1rem .1rem
    .button-wrapper
      float left
      width 31%
      .button
        margin .1rem
        padding .1rem 0
        text-align center
        border .02rem solid #ccc
        border-radius .06rem
  .item-list
    .item
      line-height .76rem
      padding-left .2rem
</style>
